package com.cms.utils;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class WebUtils {

    /**
     * 获取HttpServletRequest
     *
     * @return
     */
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request;
    }

    /**
     * 获取HttpServletResponse
     *
     * @return
     */
    public static HttpServletResponse getResponse(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return response;
    }


    /**
     * 获取HttpServletResponse
     *
     * @return
     */
    public static HttpSession getSession(){
        return getRequest().getSession();
    }

    /**
     * 获取ContextPath
     *
     * @return
     */
    public static String getContextPath(){
        return getRequest().getContextPath();
    }

    /**
     * 获取Cookie值
     *
     * @param name
     *          名称
     * @return
     */
    public static String getCookie(String name) {
        Cookie cookie = getCookieObject(name);
        return cookie != null ? cookie.getValue() : null;
    }

    /**
     * 获取Cookie
     *
     * @param name
     *          名称
     * @return
     */
    public static Cookie getCookieObject(String name) {
        Cookie[] cookies = getRequest().getCookies();
        if (cookies != null) {
            Cookie[] var3 = cookies;
            int var4 = cookies.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Cookie cookie = var3[var5];
                if (cookie.getName().equals(name)) {
                    return cookie;
                }
            }
        }

        return null;
    }


    /**
     * 删除Cookie
     *
     * @param name
     */
    public static void removeCookie(String name) {
        addCookie(name, (String)null, 0, (String)null, (String)null, (Boolean)null);
    }


    /**
     * 添加Cookie
     *
     * @param name
     * @param value
     * @param maxAgeInSeconds
     * @param path
     */
    public static void addCookie(String name, String value, int maxAgeInSeconds, String path) {
        addCookie(name,value,maxAgeInSeconds,path,null,null);
    }


    /**
     * 添加Cookie
     *
     * @param name
     * @param value
     * @param maxAgeInSeconds
     * @param path
     * @param domain
     * @param isHttpOnly
     */
    private static void addCookie(String name, String value, int maxAgeInSeconds, String path, String domain, Boolean isHttpOnly) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAgeInSeconds);
        if (path == null) {
            path = "/";
        }

        cookie.setPath(path);
        if (domain != null) {
            cookie.setDomain(domain);
        }

        if (isHttpOnly != null) {
            cookie.setHttpOnly(isHttpOnly);
        }

        getResponse().addCookie(cookie);
    }

    /**
     * 判断是否为AJAX请求
     *
     * @return 是否为AJAX请求
     */
    public static boolean isAjaxRequest() {
        HttpServletRequest request = getRequest();
        return StringUtils.equalsIgnoreCase(request.getHeader("X-Requested-With"), "XMLHttpRequest");
    }


    /**
     * 获取域名
     *
     * @return 域名
     */
    public static String getDomain(){
        HttpServletRequest request = getRequest();
        StringBuffer url = request.getRequestURL();
        String domain = url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
        domain = domain.replace("https://", "").replace("http://", "");
        return domain;
    }

}
